package com.example.login.fragment;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.login.ProductDetailsActivity;
import com.example.login.R;
import com.example.login.adapter.LeftListAdapter;
import com.example.login.adapter.RightListAdapter;
import com.example.login.entity.DataService;
import com.example.login.entity.ProductInfo;

import java.util.ArrayList;
import java.util.List;


public class HomeFragment extends Fragment {

    private View rootView;
    private RecyclerView leftRecyclerView;
    private RecyclerView rightRecyclerView;
    private LeftListAdapter leftListAdapter;
    private RightListAdapter rightListAdapter;

    private List<String> leftDataList = new ArrayList<>();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        rootView = inflater.inflate(R.layout.fragment_home, container, false);
        // 初始化控件
        leftRecyclerView = rootView.findViewById(R.id.leftRecyclerView);
        rightRecyclerView = rootView.findViewById(R.id.rightRecyclerView);

        return rootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        leftDataList.add("食品");
        leftDataList.add("饮料");
        leftDataList.add("服装");
        leftDataList.add("电器");
        leftDataList.add("日用品");
        leftDataList.add("服装");
        leftListAdapter = new LeftListAdapter(leftDataList);
        leftRecyclerView.setAdapter(leftListAdapter);

        rightListAdapter = new RightListAdapter();
        rightRecyclerView.setAdapter(rightListAdapter);
        // 默认加载食品数据
        rightListAdapter.setListData(DataService.getListData(0));

        // RecyclerView点击事件
        rightListAdapter.setOnItemClickListener(new RightListAdapter.onItemClickListener() {
            @Override
            public void onItemClick(ProductInfo productInfo, int position) {
                // 跳转详传值情页
                Intent intent = new Intent(getActivity(), ProductDetailsActivity.class);
                // intent传递对象，实体类一定要implements Serializable
                intent.putExtra("productInfo", productInfo);
                startActivity(intent);
            }
        });
        leftListAdapter.setmLeftListOnClickItemListener(new LeftListAdapter.LeftListOnClickItemListener() {
            @Override
            public void onItemClick(int position) {
//                Toast.makeText(getActivity(), position+"", Toast.LENGTH_SHORT).show();// position应该是string类型，所以加上""
                leftListAdapter.setCurrentIndex(position);
                // 点击替换分类对应页面数据
                rightListAdapter.setListData(DataService.getListData(position));
            }
        });
    }
}